package com.ljt.yupictureddd.interfaces.vo.picture;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.annotation.*;
import com.ljt.yupictureddd.domain.picture.entity.Picture;
import com.ljt.yupictureddd.domain.user.entity.User;
import com.ljt.yupictureddd.application.service.UserApplicationService;
import com.ljt.yupictureddd.interfaces.vo.user.UserVO;
import lombok.Data;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * 图片
 *
 * @TableName picture
 */
@TableName(value = "picture")
@Data
public class PictureVO implements Serializable {
    /**
     * id
     */
    private Long id;

    /**
     * 图片 url
     */
    private String url;

    /**
     * 图片名称
     */
    private String name;

    /**
     * 简介
     */
    private String introduction;

    /**
     * 分类
     */
    private String category;

    /**
     * 标签（这里是List集合，数据库是varchar存JSON 数组）
     */
    private List<String> tags;

    /**
     * 图片体积
     */
    private Long picSize;

    /**
     * 图片宽度
     */
    private Integer picWidth;

    /**
     * 图片高度
     */
    private Integer picHeight;

    /**
     * 图片宽高比例
     */
    private Double picScale;

    /**
     * 图片格式
     */
    private String picFormat;

    /**
     * 创建用户 id
     */
    private Long userId;

    /**
     * 创建时间
     */
    private Date createTime;

    /**
     * 编辑时间
     */
    private Date editTime;

    /**
     * 缩略图url
     */
    private String thumbnailUrl;

    /**
     * 更新时间
     */
    private Date updateTime;

    /**
     * 图片主色调
     */
    private String picColor;

    private UserVO userVO;
    /**
     * 权限列表：给前段展示编辑删除按钮用。
     */
    private List<String> permissionList;


    /**
     * vo转换为po的工具类
     * @param vo
     * @return
     */
    public static Picture voToObj(PictureVO vo){
        if (vo==null){
            return null;
        }
        Picture picture = BeanUtil.copyProperties(vo, Picture.class);
        picture.setTags(JSONUtil.toJsonStr(vo.tags));
        return picture;
    }

    /**
     * po转换VO的工具类
     * @param picture
     * @return
     */
    public static PictureVO objToVO(Picture picture){
        if (picture==null){
            return null;
        }
        PictureVO VO = BeanUtil.copyProperties(picture, PictureVO.class);
        VO.setTags(JSONUtil.toList(picture.getTags(),String.class));
        return VO;
    }

    /**
     * 获取PictureVO
     */
    public static PictureVO getPictureVO(Picture picture, HttpServletRequest httpServletRequest) {
        PictureVO pictureVO = objToVO(picture);
        // 关联查询用户信息
        Long userId = picture.getUserId();
        if (userId != null && userId > 0) {
            UserApplicationService userApplicationService = SpringUtil.getBean(UserApplicationService.class);
            User user = userApplicationService.getById(userId);
            UserVO userVO = userApplicationService.getUserVO(user);
            pictureVO.setUserVO(userVO);
        }
        return pictureVO;
    }
}